<template>
	<view class="container">
		<view class="controls">
			<view>
				当前BPM
				<slider min="40" max="240" step="1" :value="bpm" show-value @change="changeBPM" />
			</view>

			<picker mode="selector" :range="beatTypes" :value="beatTypeIndex" @change="changeBeatType">
				<view class="picker">
					当前拍子: {{beatTypes[beatTypeIndex]}}
				</view>
			</picker>
			<view>
				当前音量
				<slider min="0" max="100" step="1" :value="volume" show-value @change="changeVolume" />
			</view>

		</view>
		<view class="visualizer" :class="{ active: isActive }"></view>
		<button @click="toggleMetronome">{{ isPlaying ? 'Stop' : 'Start' }}</button>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				bpm: 120,
				beatTypes: ['4/4', '3/4', '2/4', '6/8'],
				beatTypeIndex: 0,
				volume: 50,
				intervalId: null,
				isPlaying: false,
				isActive: false,
				audioContext: null,
				audioBuffer: null,

			};
		},
		async onReady() {
			this.audioContext = wx.createInnerAudioContext();
			this.audioContext.src = '/static/metronome_click.wav'; // 确保你有一个点击音频文件
			this.audioContext.volume = this.volume / 100; // 设置初始音量

			// 加载音频文件
			await this.loadClickSound();
		},
		methods: {
			changeBPM(e) {
				this.bpm = e.detail.value;
				if (this.isPlaying) {
					this.stopMetronome();
					this.startMetronome();
				}
			},
			changeBeatType(e) {
				this.beatTypeIndex = e.detail.value;
			},
			changeVolume(e) {
				this.volume = e.detail.value;
				this.audioContext.volume = this.volume / 100;
			},
			toggleMetronome() {
				if (this.isPlaying) {
					this.stopMetronome();
				} else {
					this.startMetronome();
				}
			},
			startMetronome() {
				const interval = 60000 / this.bpm;
				this.intervalId = setInterval(this.playClick, interval);
				this.isPlaying = true;

			},
			stopMetronome() {
				clearInterval(this.intervalId);
				this.intervalId = null;
				this.isPlaying = false;
				this.isActive = false;

			},
			playClick() {
				this.isActive = true;
				this.audioContext.stop();
				this.audioContext.play();
				setTimeout(() => {
					this.isActive = false;
				}, 100);
				// 触发震动
				uni.vibrateShort({
					success: function() {
						console.log('震动成功');
					},
					fail: function(err) {
						console.error('震动失败:', err);
					}
				});
			},
			async loadClickSound() {
				return new Promise((resolve, reject) => {
					this.audioContext.onCanplay(() => {
						resolve();
					});
					this.audioContext.onError((err) => {
						reject(err);
					});
				});
			}
		},
	};
</script>
<style>
	.container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		height: 100vh;
		width: 100%;
		/* background-color: yellow; */
	}

	.controls {
		margin-bottom: 20px;
		width: 100%;
	}

	.picker {
		margin-top: 10px;
	}

	.slider-container {
		width: 100%;
		padding: 0 20px;
		box-sizing: border-box;
		margin-bottom: 10px;
		background-color: yellow;
	}

	.visualizer {
		width: 100px;
		height: 100px;
		border-radius: 50%;
		background-color: #4CAF50;
		transition: background-color 0.1s;
	}

	.visualizer.active {
		background-color: #f44336;
	}

	button {
		margin-top: 20px;
		padding: 10px 20px;
		background-color: #008CBA;
		color: white;
		border: none;
		border-radius: 5px;
	}
</style>